var f = require("/utils/httpPost.js"), r = require("/utils/helper/cWx.js")
var storage = require("/utils/storage.js");
App({
  onLaunch: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.windowWidth = res.windowWidth
        that.globalData.windowHeight = res.windowHeight
      }
    })
    // wx.hideTabBar();
    try {
      var o = wx.getSystemInfoSync();
      // console.log('手机信息',o)
      this.globalData.isiphonex = o.safeArea.top > 20
        //   o.model.indexOf("iPhone X") > -1
        // || o.model.indexOf("iPhone 11") > -1 
        // || o.model.indexOf("iPhone 12") > -1 
        // || o.model.indexOf("iPhone 13") > -1 
        // || o.model.indexOf("Pro Max") > -1;

      // console.log('isiphonex',this.globalData.isiphonex)
      this.globalData.isxiaomi = o.brand.indexOf("Xiaomi") > -1,
      this.globalData.isIos = o.system.toLocaleLowerCase().indexOf("ios") > -1;
    } catch (e) {
      console.log(e);
    }

    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  //设置tabbar的选中
  getCurrentTabbar(selected, that) {
    if (typeof that.getTabBar === 'function' && that.getTabBar()) {
        that.getTabBar().setData({
            selected: selected
        })
    }
  },
  getCPages: function () {
    return getCurrentPages()[getCurrentPages().length - 1];
  },
  getUrl: r.getUrl,
  setLoading: function (e, t, n) {
    e && e.__route__ || (t, t = e, e = this.getCPages()), e.setData({
      loading_isLoading: t
    });
  },
  setMessage: function (e, t, n) {
    e && e.__route__ || (n = t, t = e, e = this.getCPages()), e.setData({
      message_isShow: !0,
      message_text: t
    }, n), clearTimeout(e.setMessage_timer), e.setMessage_timer = setTimeout(function () {
      e.setData({
        message_isShow: !1
      });
    }, 2e3);
  },
  goUrl: function (e) {
    console.log('goUrl e',e)
    if (e && !/^http(s|)/.test(e)) {
      var urls = ["pages/home/home", "pages/personal/index", "pages/shoppingcar/shoppingcar", "pages/producttype/producttype"]
      for (var t = urls, n = 0; n < t.length; n++) 
      if (e.indexOf(t[n]) > -1) {
        return void
          wx.switchTab({
            url: "/" + t[n]
          });
      } 
      wx.navigateTo({
        url: e
      });
    }
  },
  getImgUrl:function(){
    return f.getImgUrl();
  },
  getUserId:function(){
    return storage.storage.getStorageSync("user_id");
  },
  getCurrentTitle: function () {
    var e = this.getCPages();
    return e.static && e.static.title ? e.static.title : "兵哥山泉";
  },
  saTrack: function (t, n) {
    var e = this.getCPages();
    e.default.track(t, n);
  },
  setStorageSync: function (e, t) {
    storage.storage.setStorageSync(e,t)
  },
  getStorageSync: function (e) {
    return storage.storage.getStorageSync(e)
  },
  removeStorageSync: function (e) {
    storage.storage.removeStorageSync(e)
  },
  getUserInfoById:function(user_id){
    var that = this
    f.httpPost('/user/getUserInfoById', { user_id: user_id }, function (res) {
      console.log("登录返回数据", res)
      if(res.data.code == 0){
        console.log('res.data.data.is_reLogin',res.data.data.is_reLogin)
        if(res.data.data.is_reLogin == 0){
          that.globalData.user_id = res.data.data.user_id;
          that.globalData.is_mer = res.data.data.is_mer;
          that.setStorageSync("isLogin",1);
          that.setStorageSync("user_id", res.data.data.user_id);
          that.setStorageSync("nick_name", res.data.data.nick_name);
          that.setStorageSync("face_url", res.data.data.face_url);
          that.setStorageSync("access_token", res.data.data.access_token);
          that.setStorageSync("is_mer", res.data.data.is_mer);
          that.setStorageSync("shop_id", res.data.data.shop_id);
          that.setStorageSync("is_admin", res.data.data.is_admin);
          that.setStorageSync("phone_num", res.data.data.phone_num);
          that.setStorageSync("estate_id", res.data.data.estate_id);
          that.setStorageSync("open_id", res.data.data.open_id);
          that.setStorageSync("is_new_user", res.data.data.is_new_user);
          that.setStorageSync("is_focus_mp",0);
          if(res.data.data.is_focus_mp!=undefined){
            that.setStorageSync("is_focus_mp", res.data.data.is_focus_mp);
          }
        }else{
          console.log('==============')
          that.setStorageSync("isLogin",0);
          that.setStorageSync("user_id", 0);
        }
        // that.getUserInfoByOpenId(res.data.data.open_id);
      }else{
        that.setStorageSync("isLogin",0);
        that.setStorageSync("user_id", 0);
      }
    }, function () { })
  },
  getUserInfoByOpenId:function(open_id){
    var that = this
    f.httpPost('/user/getUserInfoByOpenId', { open_id: open_id }, function (res) {
      console.log("getUserInfoByOpenId 登录返回数据", res)
      if(res.data.code == 0){
        that.globalData.user_id = res.data.data.user_id;
        that.globalData.is_mer = res.data.data.is_mer;
        that.setStorageSync("isLogin",1);
        that.setStorageSync("user_id", res.data.data.user_id);
        that.setStorageSync("access_token", res.data.data.access_token);
        that.setStorageSync("is_mer", res.data.data.is_mer);
        that.setStorageSync("shop_id", res.data.data.shop_id);
        that.setStorageSync("is_admin", res.data.data.is_admin);
        that.setStorageSync("phone_num", res.data.data.phone_num);
        that.setStorageSync("estate_id", res.data.data.estate_id);
        that.setStorageSync("is_focus_mp",0);
        if(res.data.data.is_focus_mp != undefined){
          that.setStorageSync("is_focus_mp", res.data.data.is_focus_mp);
        }
      }
    }, function () { })
  },
  globalData: {
    userInfo: null,
    is_mer:0,
    is_admin: 0,
    shop_type: 0,
    is_super_admin:0,
    authSetting:0,
    user_id:0,
    isiphonex: false,
    isxiaomi: false,
    isIos:false,
    windowWidth: 0,
    windowHeight: 0,
    isLogin:0,
    openId:'',
    to_farm_user_id:0,
    ag_id:0,
    promoter_id:0,
  },
  fetchApi: f,
})